import {useAuthStore} from "@/store/auth";

const checkPermission = (el:any, binding:any) => {
    // console.log(el)
    // console.log(binding)
    if (!hasPermission(binding.value)) {
        el.parentNode && el.parentNode.removeChild(el);
    }
}

export const hasPermission = (value:any): boolean => {
    const auth = useAuthStore()
    const permissions = auth.permissions ?? [];
    if (auth.isSuperAdmin) {
        return true;
    }
    if (typeof value === 'string') {
        return permissions.includes(value);
    }
    return false;
}

export const permission = {
    mounted(el:any, binding:any) {
        checkPermission(el, binding);
    },
    updated(el:any, binding:any) {
        checkPermission(el, binding);
    },
};